import { GlobalStore } from "../../store/global";
import { Task } from "../../store/types";
import { HighlightHelper } from "../../utils/HighlightHelper";
import { Logger } from "../../utils/Logger";
import { MessageHandler, MessageType } from "../../utils/MessageHandler";
import { AgentViewController } from "../AgentViewController";

/**
 * 消息处理器
 * 用于处理本地存储的消息
 */
export class StoreMessageHandler implements MessageHandler {
	highlightHelper: HighlightHelper;
	constructor(private readonly controller: AgentViewController) {
		this.highlightHelper = new HighlightHelper(this.controller);
	}
	handleMessage(message: any): void {
		Logger.log('AI Code Agent: handle store message');
		switch(message.type) {
			case MessageType.getConfig:
				{
					const config = GlobalStore.instance.getConfig();
					if(!config.highlightjs) {
						config.highlightjs = this.highlightHelper.getTheme();
					}
					this.controller.sendMessageToWebView({type: MessageType.getConfig, data: config});
					break;
				}
			case MessageType.setConfig:
				{
					GlobalStore.instance.setConfig(message.data);
					break;
				}
			case MessageType.getHistory:
				{
					const tasks: Task[] = GlobalStore.instance.getHistory();
					this.controller.sendMessageToWebView({type: MessageType.getHistory, data: tasks});
					break;
				}
			case MessageType.getTask:
				{
					const task = GlobalStore.instance.getTask(message.data.taskId);
					this.controller.sendMessageToWebView({type: MessageType.getTask, data: task});
					break;
				}
			case MessageType.getThemeContent:
				{
					const themeContent = this.highlightHelper.getThemeContent(message.data.themeName);
					this.controller.sendMessageToWebView({type: MessageType.getThemeContent, data: themeContent});
					break;
				}
			case MessageType.getHighlightjs:
				{
					const highlightjs =  GlobalStore.instance.getHighlightjs();
					this.controller.sendMessageToWebView({type: MessageType.getHighlightjs, data: highlightjs});
					break;
				}
		}
	}
}